<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    width="63%"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="网站名称" prop="name"
                :rules="[
                  {required: true, message:'网站名称不能为空', trigger:'blur'}
                ]">
              <el-input v-model="inputForm.name" placeholder="请填写网站名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="网站标题" prop="title"
                :rules="[
                  {required: true, message:'网站标题不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.title" placeholder="请填写网站标题"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="网站副主题" prop="subtitle"
                :rules="[
                 ]">
              <el-input v-model="inputForm.subtitle" placeholder="请填写网站副主题"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="关联主题" prop="theme.id"
                        :rules="[
                          {required: true, message:'关联主题不能为空', trigger:'blur'}
                 ]">
            <el-select v-model="inputForm.theme.id" placeholder="请选择主题">
              <el-option
                v-for="item in themeList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="网站域名" prop="domain"
                :rules="[
                 ]">
              <el-input v-model="inputForm.domain" placeholder="请填写网站域名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="网站简述" prop="description"
                :rules="[
                  {required: true, message:'简述不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.description" placeholder="请填写网站简述"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="关键字" prop="keywords"
                :rules="[
                  {required: true, message:'关键字不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.keywords" placeholder="请填写关键字"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="版权信息" prop="copyright"
                :rules="[
                  {required: true, message:'版权信息不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.copyright" placeholder="请填写版权信息"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="自定义首页视图" prop="customIndexView"
                :rules="[
                 ]">
              <el-input v-model="inputForm.customIndexView" placeholder="请填写自定义首页视图"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="电话" prop="phone"
                :rules="[
                  {required: true, message:'电话不能为空', trigger:'blur'},
                  {validator: validator.isPhoneOrMobile, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.phone" placeholder="请填写电话"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="传真" prop="fax"
                :rules="[
                 ]">
              <el-input v-model="inputForm.fax" placeholder="请填写传真"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="QQ" prop="qq"
                :rules="[
                 ]">
              <el-input v-model="inputForm.qq" placeholder="请填写QQ"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="微信" prop="wechat"
                :rules="[
                 ]">
              <el-input v-model="inputForm.wechat" placeholder="请填写微信"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="Email" prop="email"
                :rules="[
                  {validator: validator.isEmail, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.email" placeholder="请填写Email"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="地址" prop="address"
                :rules="[
                  {required: true, message:'地址不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.address" placeholder="请填写地址"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="经度" prop="longitude"
                :rules="[
                  {validator: validator.isFloatNEqZero, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.longitude" placeholder="请填写经度"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="纬度" prop="latitude"
                :rules="[
                  {validator: validator.isFloatNEqZero, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.latitude" placeholder="请填写纬度"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="版本号" prop="siteVersion"
                        :rules="[
                          {required: true, message:'版本号不能为空', trigger:'blur'},
                  {validator: validator.isVersion, trigger:'blur'}
                 ]">
            <el-input v-model="inputForm.siteVersion" placeholder="请填写版本号x.x.x"     ></el-input>
          </el-form-item>
        </el-col>


        <el-col :span="12">
          <el-form-item label="序号" prop="sort"
                        :rules="[
                          {required: true, message:'序号不能为空', trigger:'blur'}
                 ]">
            <el-input-number size="small" v-model="inputForm.sort" :min="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="微信二维码" prop="wechatQrcode">
            <multi-upload v-model="inputForm.wechatQrcode"
                          upload-directory="/cms/link/wechatQrcode"
                          :limit="1"></multi-upload>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item label="网站Logo" prop="logo">
            <multi-upload v-model="inputForm.logo"
                          upload-directory="/cms/link/logo"
                          :limit="1"></multi-upload>
          </el-form-item>
        </el-col>
        <el-col>
          <Map @getPosition="getPosition" :input-position="{lng:inputForm.longitude,lat:inputForm.latitude,address:inputForm.address}"></Map>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>


<script>
import SelectTree from '@/components/treeSelect/treeSelect.vue'
import MultiUpload from '@/components/multiUpload/multiUpload.vue'
import Map from '@/components/aMap/map.vue'
export default {
  props: {
    themeList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      inputForm: {
        id: '',
        name: '',
        title: '',
        subtitle: '',
        logo: '',
        domain: '',
        description: '',
        keywords: '',
        copyright: '',
        customIndexView: '',
        phone: '',
        fax: '',
        qq: '',
        wechat: '',
        wechatQrcode: '',
        email: '',
        address: '',
        longitude: '',
        latitude: '',
        homeHits: '',
        remarks: '',
        sort: 0,
        siteVersion: '',
        theme: {
          id: '',
          name: ''
        }
      }
    }
  },
  components: {
    SelectTree,
    MultiUpload,
    Map
  },
  methods: {
    init (method, id) {
      this.method = method
      this.inputForm.id = id
      if (method === 'add') {
        this.title = `新建网站`
      } else if (method === 'edit') {
        this.title = '修改网站'
      } else if (method === 'view') {
        this.title = '查看网站'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          this.$http({
            url: `/cms/site/cmsSite/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data.cmsSite)
            console.log('longitude:' + this.inputForm.longitude)
            this.loading = false
          })
        }
      })
    },
      // 表单提交
    doSubmit () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/cms/site/cmsSite/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            this.loading = false
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
          })
        }
      })
    },
    getPosition (lng, lat, address) {
      this.inputForm.longitude = lng
      this.inputForm.latitude = lat
      this.inputForm.address = address
    }
  }
}
</script>
